﻿using System;
using System.Collections.Generic;
using Castle.ActiveRecord;

namespace NoMiddleman.Core.Domain
{
    [ActiveRecord(DiscriminatorColumn = "Type", DiscriminatorType = "String", DiscriminatorValue = "Client")]
    public class Client
    {
        [PrimaryKey]
        public int Id { get; protected set; }

        [Property]
        public string Login { get; set; }

        [Property]
        public string Password { get; set; }

        [Property]
        public decimal Credit { get; set; }

        [BelongsTo(Type = typeof(Address))]
        public Address Address { get; set; }

        [Property]
        public DateTime DateRegistered { get; set; }

        [HasMany(typeof(Advert))]
        public IList<Advert> Adverts { get; set; }

        public bool IsRegularClient()
        {
            if (Adverts == null) return false;
            return Adverts.Count > 10;
        }

        public bool TryAddAdvert(Advert advert)
        {
            decimal price = advert.CalculateCost();
            if (price > Credit) return false;
            Credit -= price;
            Adverts.Add(advert);
            advert.Client = this;
            return true;
        }
    }
}